package com.gzhuxj201.exception;

import com.gzhuxj201.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result exceptionHandle(Exception ex){
        log.info("错误信息:"+ ex.getMessage());
        if(ex.getMessage().contains("Duplicate entry")){//新增员工id和数据库已有员工的id重复的异常
            String[] split=ex.getMessage().split(" ");
            String msg=split[9]+"已存在";
            return Result.error(msg);
        }
        else if(ex.getMessage().contains("cannot be null")){
            String[] split=ex.getMessage().split(" ");
            String msg=split[8]+"不能为空";
            return Result.error(msg);
        }

        ex.printStackTrace();
        return Result.error("操作错误,请联系管理员");
    }

  /*  @ExceptionHandler({SQLIntegrityConstraintViolationException.class})
    public Result exceptionHandler(SQLIntegrityConstraintViolationException ex){

        if(ex.getMessage().contains("Duplicate entry")){//新增员工id和数据库已有员工的id重复的异常
            String[] split=ex.getMessage().split(" ");
            String msg=split[2]+"已存在";
            return Result.error(msg);
        }
        else if(ex.getMessage().contains("cannot be null")){
            String[] split=ex.getMessage().split(" ");
            String msg=split[1]+"不能为空";
            return Result.error(msg);
        }
        return Result.error("未知错误");
    }*/


}
